package com.car.dao.impl;

import com.car.dao.UserDao;
import com.car.domain.Car;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import com.car.utils.JDBCUtils;

import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public int add(User user) throws Exception {
        String sql="insert into t_user(name,password,phone,email)"+
                " values(?,?,?,?)";
        return JDBCUtils.update(sql,user.getName(),user.getPassword(),user.getPhone(),user.getEmail());

    }

    @Override
    public int delete(int id) throws Exception {
        String sql="delete from t_user where id=?";
        return JDBCUtils.update(sql,id);
    }

    @Override
    public int update(User user) throws Exception {
        String sql="update t_user set name=?,password=?,phone=?,email=?"+
                "where id=?";

        return JDBCUtils.update(sql,user.getName(),user.getPassword(),user.getPhone(),user.getEmail(),user.getId());


    }

    @Override
    public User queryById(int id) throws Exception {
        String sql="select * from t_user where id=?";
        return JDBCUtils.query(sql, User.class,id);

    }

    @Override
    public List<User> queryAll() throws Exception {
        String sql="select * from t_user";
        return JDBCUtils.querylist(sql,User.class);
    }

    @Override
    public boolean register(User user) {
        //写之前去判断，手机号是否被注册过
        try {
            User byPhone = findByPhone(user.getPhone());
            if (byPhone != null) {
                return false;
            }
            //新增用户
            add(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
           throw new RuntimeException("注册异常");
        }
    }

    /**
     * 通过手机号查询用户
     *
     * @param phone 手机号
     * @return 用户
     */
    @Override
    public User findByPhone(String phone) throws Exception {
        String sql="select * from t_user where phone=?";
        return JDBCUtils.query(sql,User.class,phone);
    }

    /**
     * 用户通过手机号 密码 登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return 用户
     */
    @Override
    public User login(String phone, String password) {

        try {
            String sql="select * from t_user where phone=? and password=?";
            return  JDBCUtils.query(sql,User.class,phone,password);
        } catch (Exception e) {
         e.printStackTrace();
        }
        return null;
    }

    /**
     * 查看自己的汽车
     *
     * @param id id
     * @return 汽车
     */
    @Override
    public List<Car> ownCars(Integer userId) {
        try {
            String sql="select * from t_user_car tuc left join " +
                    "t_car tc on tuc.car_id=tc.id where tuc.user_id=?";
            return JDBCUtils.querylist(sql,Car.class,userId);
        } catch (Exception e) {
          e.printStackTrace();
        }
        return null;
    }

    /**
     * 查看购买记录
     *
     * @param id
     * @return
     */
    @Override
    public List<PurchaseRecord> getPurchaseRecords(Integer id) {
        try {
            String sql="select * from t_purchase_record where user_id=?";
            return JDBCUtils.querylist(sql,PurchaseRecord.class,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
